package servlet;

import dao.SetLeaveDao;
import text.leaveBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/setLeaveServlet")
public class SetLeaveServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        //获取从留言板输入来的内容
        String leave = request.getParameter("leave");
        if (leave == "") {
            request.setAttribute("EmptyMessages", "留言为空");
            request.getRequestDispatcher("/leaveMessage.jsp").forward(request, response);
            return;
        }
        //将leave存储leaveBean中
        leaveBean leaveBean = new leaveBean();
        //将留言存在leaveBean中
        leaveBean.setLeave(leave);
        //获取当地时间，并存储在leaveBean中
        leaveBean.setTime(new Date().toLocaleString());
        //获取用户名存在leaveBean中
        leaveBean.setUser((String) request.getSession().getAttribute("user"));
        //使用SetLeaveDao将留言存储在数据库
        new SetLeaveDao().setLeave(leaveBean);
        response.sendRedirect(request.getContextPath() + "/findAllServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
